﻿/*
版权所有：版权所有(C) 2012，321Nets
文件编号：M01_ProductsDao.cs
文件名称：ProductsBLL.cs
系统编号：Z0003007
系统名称：通用办公系统
模块编号：M01
模块名称：信息发布
设计文档：IOA_M01信息发布XDE建模.mdx
完成日期：2012-5-5
作　　者：
内容摘要：表[Purchase]对应的业务逻辑 BLL
*/
using System;
using System.Data;
using System.Data.OleDb;
using System.Collections;
using System.Configuration;


using Eis51.CampanyCMS.Model.Bus;

namespace Eis51.CampanyCMS.Access.Bus
{
    /// <summary>
    /// Purchase的BLL
    /// 该BLL已经生成了基本的业务操作，在自动生成的方法中已经提供了事务机制
    /// </summary>
    public class PurchaseDao
    {
        #region 自动生成代码：取得相关实体

        //数据库连接字符串(web.config来配置)
        //<add key="ConnectionString" value="Provider=Microsoft.Jet.OLEDB.4.0; Data Source=D:\\db1.mdb" />

        protected static string connectionString =
            string.Format(ConfigurationSettings.AppSettings["Eis_ConnectionString"],
                          System.Web.HttpContext.Current.Request.PhysicalApplicationPath);

        /// <summary>
        /// 方法名称: PurchaseDao
        /// 内容摘要: 构造函数进行初始化
        /// </summary>
        protected PurchaseDao()
        {
        }

        private static volatile PurchaseDao m_instance = null;
        /// <summary>
        /// 方法名称: GetInstance
        /// 内容摘要: 取得 PurchaseDao 对象
        /// </summary>
        /// <returns>返回一个已经存在的实体</returns>
        public static PurchaseDao GetInstance()
        {
            // 通用的必要代码 iBatisNet双校检机制,如果实例不存在
            if (m_instance == null)
            {
                lock (typeof(PurchaseDao))
                {

                    // 如果实例不存在
                    if (m_instance == null)
                    {

                        // 创建一个的实例
                        m_instance = new PurchaseDao();
                    }
                }
            }

            // 返回业务逻辑对象
            return m_instance;
        }

        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dataReader">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        //private static Purchase DataReaderToEntity(IDataReader dataReader)
        //{
        //	Purchase obj = null;			
        //    while (dataReader.Read())
        //    {
        //		obj = new Purchase();

        //
        //	}
        //	return obj;
        //}	
        /// <summary>
        /// 将DataReader的属性值转化为实体类的属性值，返回实体类
        /// </summary>
        /// <param name="dataReader">有效的DataReader对象</param>
        /// <returns>实体类对象</returns>
        private Purchase DataRowToEntity(DataRow dataReader)
        {
            Purchase obj = new Purchase();


            if (dataReader["Seqno"] != DBNull.Value) obj.Seqno = (int)dataReader["Seqno"];
            if (dataReader["ProductID"] != DBNull.Value) obj.ProductID = (int)dataReader["ProductID"];
            if (dataReader["Title"] != DBNull.Value) obj.Title = (string)dataReader["Title"];
            if (dataReader["Type"] != DBNull.Value) obj.Type = (int)dataReader["Type"];
            if (dataReader["Descr"] != DBNull.Value) obj.Descr = (string)dataReader["Descr"];
            if (dataReader["feature"] != DBNull.Value) obj.Feature = (string)dataReader["feature"];
            if (dataReader["ShortDescr"] != DBNull.Value) obj.Shortdescr = (string)dataReader["ShortDescr"];
            if (dataReader["price"] != DBNull.Value) obj.Price = (double)dataReader["price"];
            if (dataReader["Nowprice"] != DBNull.Value) obj.Nowprice = (double)dataReader["Nowprice"];
            if (dataReader["Sore"] != DBNull.Value) obj.Sore = (double)dataReader["Sore"];
            if (dataReader["KeyWord"] != DBNull.Value) obj.Keyword = (string)dataReader["KeyWord"];
            if (dataReader["Isfirst"] != DBNull.Value) obj.Isfirst = (int)dataReader["Isfirst"];
            if (dataReader["Level"] != DBNull.Value) obj.Level = (int)dataReader["Level"];
            if (dataReader["Createby"] != DBNull.Value) obj.Createby = (string)dataReader["Createby"];
            if (dataReader["CreateDate"] != DBNull.Value) obj.Createdate = (DateTime)dataReader["CreateDate"];
            if (dataReader["LasteUpdateby"] != DBNull.Value) obj.Lasteupdateby = (string)dataReader["LasteUpdateby"];
            if (dataReader["LasteUpdateDate"] != DBNull.Value) obj.Lasteupdatedate = (DateTime)dataReader["LasteUpdateDate"];
            if (dataReader["Licd"] != DBNull.Value) obj.Licd = (int)dataReader["Licd"];
            if (dataReader["Img_Big_Path"] != DBNull.Value) obj.ImgBigPath = (string)dataReader["Img_Big_Path"];
            if (dataReader["Img_Smaill_Path"] != DBNull.Value) obj.ImgSmaillPath = (string)dataReader["Img_Smaill_Path"];
            if (dataReader["File_List_ID"] != DBNull.Value) obj.FileListId = (int)dataReader["File_List_ID"];
            if (dataReader["Code"] != DBNull.Value) obj.Code = (string)dataReader["Code"];
            if (dataReader["Mail_Price"] != DBNull.Value) obj.MailPrice = (double)dataReader["Mail_Price"];
            if (dataReader["Stocks"] != DBNull.Value) obj.Stocks = (int)dataReader["Stocks"];
            if (dataReader["Indeximg"] != DBNull.Value) obj.Indeximg = (string)dataReader["Indeximg"];
            if (dataReader["OtherDescr"] != DBNull.Value) obj.Otherdescr = (string)dataReader["OtherDescr"];

            return obj;
        }

        /// <summary>
        /// 插入对象
        /// </summary>
        /// <param name="entity">
        /// 实体里的主键被赋值
        ///	</param>
        /// <returns>
        /// 返回当前插入的主键值
        ///</returns>
        public int Insert(Purchase entity)
        {
            int returnValue = -1;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {
                try
                {

                    OleDbParameter p;
                    string sqlCommand = "Insert into Purchase(ProductID,[Title],[Type],[Descr],[feature],[ShortDescr],[price],[Nowprice],[Sore],[KeyWord],[Isfirst],[Level],[Createby],[CreateDate],[LasteUpdateby],[LasteUpdateDate],[Licd],[Img_Big_Path],[Img_Smaill_Path],[File_List_ID],[Code],[Mail_Price],[Stocks],[Indeximg],[OtherDescr]) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@ProductID", OleDbType.Integer);
                        p.Value = entity.ProductID;
                        cmd.Parameters.Add(p);

                        p = new OleDbParameter("@Title", OleDbType.VarChar);
                        p.Value = entity.Title;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Type", OleDbType.Integer);
                        p.Value = entity.Type;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Descr", OleDbType.VarChar);
                        p.Value = entity.Descr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@feature", OleDbType.VarChar);
                        p.Value = entity.Feature;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@ShortDescr", OleDbType.VarChar);
                        p.Value = entity.Shortdescr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@price", OleDbType.Double);
                        p.Value = entity.Price;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Nowprice", OleDbType.Double);
                        p.Value = entity.Nowprice;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Sore", OleDbType.Double);
                        p.Value = entity.Sore;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@KeyWord", OleDbType.VarChar);
                        p.Value = entity.Keyword;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Isfirst", OleDbType.Integer);
                        p.Value = entity.Isfirst;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Level", OleDbType.Integer);
                        p.Value = entity.Level;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Createby", OleDbType.VarChar);
                        p.Value = entity.Createby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@CreateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Createdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateby", OleDbType.VarChar);
                        p.Value = entity.Lasteupdateby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Lasteupdatedate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Licd", OleDbType.Integer);
                        p.Value = entity.Licd;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Img_Big_Path", OleDbType.VarChar);
                        p.Value = entity.ImgBigPath;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Img_Smaill_Path", OleDbType.VarChar);
                        p.Value = entity.ImgSmaillPath;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@File_List_ID", OleDbType.Integer);
                        p.Value = entity.FileListId;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Code", OleDbType.VarChar);
                        p.Value = entity.Code;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Mail_Price", OleDbType.Double);
                        p.Value = entity.MailPrice;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Stocks", OleDbType.Integer);
                        p.Value = entity.Stocks;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Indeximg", OleDbType.VarChar);
                        p.Value = entity.Indeximg;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@OtherDescr", OleDbType.VarChar);
                        p.Value = entity.Otherdescr;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();

                        sqlCommand = "SELECT TOP 1 Seqno FROM Purchase ORDER BY Seqno DESC";

                        cmd.CommandText = sqlCommand;
                        object o = cmd.ExecuteScalar();
                        entity.Seqno = Convert.ToInt32(o); ;
                        returnValue = entity.Seqno;
                    }
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
            return returnValue;
        }
        /// <summary>
        /// 更新对象
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(Purchase entity)
        {
            bool returnValue = false;
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                try
                {
                    OleDbParameter p;
                    string sqlCommand = "Update Purchase Set ProductID=?, [Title] = ?,[Type] = ?,[Descr] = ?,[feature] = ?,[ShortDescr] = ?,[price] = ?,[Nowprice] = ?,[Sore] = ?,[KeyWord] = ?,[Isfirst] = ?,[Level] = ?,[Createby] = ?,[CreateDate] = ?,[LasteUpdateby] = ?,[LasteUpdateDate] = ?,[Licd] = ?,[Img_Big_Path] = ?,[Img_Smaill_Path] = ?,[File_List_ID] = ?,[Code] = ?,[Mail_Price] = ?,[Stocks] = ?,[Indeximg] = ?,[OtherDescr] = ? where [Seqno] =?  ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@ProductID", OleDbType.Integer);
                        p.Value = entity.ProductID;
                        cmd.Parameters.Add(p);

                        p = new OleDbParameter("@Title", OleDbType.VarChar);
                        p.Value = entity.Title;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Type", OleDbType.Integer);
                        p.Value = entity.Type;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Descr", OleDbType.VarChar);
                        p.Value = entity.Descr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@feature", OleDbType.VarChar);
                        p.Value = entity.Feature;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@ShortDescr", OleDbType.VarChar);
                        p.Value = entity.Shortdescr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@price", OleDbType.Double);
                        p.Value = entity.Price;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Nowprice", OleDbType.Double);
                        p.Value = entity.Nowprice;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Sore", OleDbType.Double);
                        p.Value = entity.Sore;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@KeyWord", OleDbType.VarChar);
                        p.Value = entity.Keyword;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Isfirst", OleDbType.Integer);
                        p.Value = entity.Isfirst;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Level", OleDbType.Integer);
                        p.Value = entity.Level;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Createby", OleDbType.VarChar);
                        p.Value = entity.Createby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@CreateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Createdate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateby", OleDbType.VarChar);
                        p.Value = entity.Lasteupdateby;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@LasteUpdateDate", OleDbType.DBTimeStamp);
                        p.Value = DateTime.Parse(entity.Lasteupdatedate.ToString("yyyy-MM-dd HH:mm:ss"));
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Licd", OleDbType.Integer);
                        p.Value = entity.Licd;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Img_Big_Path", OleDbType.VarChar);
                        p.Value = entity.ImgBigPath;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Img_Smaill_Path", OleDbType.VarChar);
                        p.Value = entity.ImgSmaillPath;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@File_List_ID", OleDbType.Integer);
                        p.Value = entity.FileListId;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Code", OleDbType.VarChar);
                        p.Value = entity.Code;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Mail_Price", OleDbType.Double);
                        p.Value = entity.MailPrice;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Stocks", OleDbType.Integer);
                        p.Value = entity.Stocks;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Indeximg", OleDbType.VarChar);
                        p.Value = entity.Indeximg;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@OtherDescr", OleDbType.VarChar);
                        p.Value = entity.Otherdescr;
                        cmd.Parameters.Add(p);
                        p = new OleDbParameter("@Seqno", OleDbType.Integer);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();
                    }
                    returnValue = true;
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                }
            }
            return returnValue;
        }

        /// <summary>
        /// 通过主键取得实体
        /// </summary>
        /// <param name="entity"> 主键 </param>
        /// <returns>实体对象</returns>
        public Purchase SelectByID(Purchase.Key entity)
        {
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                try
                {
                    OleDbParameter p;
                    string sqlCommand = " select * From Purchase where [Seqno] = ?    ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Seqno", DbType.Int32);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                        adp.SelectCommand = cmd;
                        DataTable dt = new DataTable();
                        adp.Fill(dt);
                        if (dt.Rows.Count > 0)
                        {
                            return DataRowToEntity(dt.Rows[0]);
                        }
                        else
                        {
                            return null;
                        }
                    }
                }
                catch
                {
                    if (connection.State == ConnectionState.Open)
                    {
                        connection.Close();
                    }
                    return null;
                }
            }
        }

        public IList SelectAll()
        {
            ArrayList ar = new ArrayList();
            using (OleDbConnection connection = new OleDbConnection(connectionString))
            {

                string sqlCommand = "   select * From Purchase  ";
                using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                {
                    connection.Open();
                    OleDbDataAdapter adp = new OleDbDataAdapter(sqlCommand, connection);
                    adp.SelectCommand = cmd;
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    foreach (DataRow dr in dt.Rows)
                    {
                        ar.Add(DataRowToEntity(dr));
                    }
                    return ar;
                }
            }

        }
        public bool DeleteByID(Purchase.Key entity)
        {
            try
            {
                using (OleDbConnection connection = new OleDbConnection(connectionString))
                {

                    OleDbParameter p;
                    string sqlCommand = " delete From Purchase where [Seqno] = ?    ";
                    using (OleDbCommand cmd = new OleDbCommand(sqlCommand, connection))
                    {
                        connection.Open();

                        p = new OleDbParameter("@Seqno", DbType.Int32);
                        p.Value = entity.Seqno;
                        cmd.Parameters.Add(p);
                        cmd.ExecuteNonQuery();
                        return true;
                    }
                }
            }
            catch
            {
                return false;
            }
        }
        #endregion

    }
}
